//
//  MineViewController.m
//  DS_PJ_01
//
//  Created by 宋畅 on 2016/12/15.
//  Copyright © 2016年 zhiqiyun. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "MineFooterView.h"
#import "SchedulingViewController.h"
#import "WebViewController.h"

@interface MineViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,strong)NSMutableArray *listArray;

@property(nonatomic,strong)MineHeaderView *headerView;

@property(nonatomic,strong)MineFooterView *footerView;
@property(nonatomic,strong)UIActionSheet *applySheet;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"My Management", @"我的管理");
    
    self.tableView.height -= 30;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;
}


-(NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]initWithObjects:@"我的信息",@"管理",@"申请",@"排班",@"客评",@"意见", nil];
    }
    return _listArray;
}

-(MineHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[MineHeaderView alloc] init];
        __weak MineViewController *self_weak=self;
        _headerView.headImageClick=^(){
            UIActionSheet *applySheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self_weak cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
            applySheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            [applySheet showInView:self_weak.view];
        };
    }
    return _headerView;
}

-(MineFooterView *)footerView
{
    if (!_footerView) {
        _footerView = [[MineFooterView alloc] init];
    }
    return _footerView;
}

#pragma mark -------- tableView delegate ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *mineCellIdetifier = @"mineCellIdetifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCellIdetifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:mineCellIdetifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = self.listArray[indexPath.row];
    
    if (indexPath.row == 0) {
        cell.textLabel.font = FONT(20);
    } else {
        cell.textLabel.font = FONT(17);
    }
    
    
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.listArray[indexPath.row] isEqualToString:@"我的信息"]) {
        WebViewController *webVC=[[WebViewController alloc]init];
        webVC.webViewURLString=@"http://chief-media.com/html/waiter/employee_oneself.html";
        webVC.title=@"我的信息";
        [self.navigationController pushViewController:webVC animated:YES];
    }
    else if ([self.listArray[indexPath.row] isEqualToString:@"管理"]) {
        SchedulingViewController *vc  =[[SchedulingViewController alloc]init];
        vc.title = self.listArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([self.listArray[indexPath.row] isEqualToString:@"申请"]){
        self.applySheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"请假",@"调休",@"换班", nil];
        self.applySheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [self.applySheet showInView:self.view];
    }
    else if ([self.listArray[indexPath.row] isEqualToString:@"排班"]){
        WebViewController *webVC=[[WebViewController alloc]init];
        webVC.webViewURLString=@"http://chief-media.com/html/waiter/employee_paiban_info.html";
        webVC.title=@"排班";
        [self.navigationController pushViewController:webVC animated:YES];
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet==self.applySheet) {//申请
        switch (buttonIndex) {
            case 0:
            {//请假
                WebViewController *webVC=[[WebViewController alloc]init];
                webVC.title=@"请假";
                webVC.webViewURLString=@"http://chief-media.com/html/waiter/employee_leave_shenqing.html";
                [self.navigationController pushViewController:webVC animated:YES];
            }
                break;
            case 1:{//调休
                WebViewController *webVC=[[WebViewController alloc]init];
                webVC.title=@"调休";
                webVC.webViewURLString=@"http://chief-media.com/html/waiter/employee_tiaoxiu.html";
                [self.navigationController pushViewController:webVC animated:YES];
            }
                break;
            case 2:
            {//换班
                WebViewController *webVC=[[WebViewController alloc]init];
                webVC.title=@"换班";
                webVC.webViewURLString=@"http://chief-media.com/html/waiter/employee_shifts.html";
                [self.navigationController pushViewController:webVC animated:YES];
            }
                break;
            default:
                break;
        }
    }
    else{//选择头像
        switch (buttonIndex) {
            case 0:
            {
                if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                    imagePicker.delegate = self;
                    imagePicker.allowsEditing = YES;
                    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [self presentViewController:imagePicker animated:YES completion:nil];
                }
            }
                break;
            case 1:
            {
                UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:imagePicker animated:YES completion:nil];
            }
                break;
                
            default:
                break;
        }
    }
}

- ( void)imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo:( NSDictionary *)info
{
    [_headerView refreshHeadVew:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];
    ///这里需要做上传工作
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
